package com.libing.service.impl;

import com.libing.dao.CompanyDao;
import com.libing.dao.CustomerDao;
import com.libing.dao.InvoiceInformationDao;
import com.libing.entity.*;
import com.libing.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;

/**
 * @Description:
 * @Author: WangY
 * @Date: Created in 2018-08-31 22:54
 * @Modified By：
 */

@Repository
public class TranService {

    @Autowired
    private CustomerDao customerDao;

    @Autowired
    private CompanyDao companyDao;

    @Autowired
    private InvoiceInformationDao invoiceInformationDao;

    @Transactional(rollbackFor = Exception.class)
    public Customer splitInfo(CustomerAllInfo customerAllInfo, User user) {

        Timestamp nowTime = TimeUtil.getNewTime();
        Customer customer = new Customer();
        Company company = new Company();
        InvoiceInformation invoiceInformation = new InvoiceInformation();

        if (customerAllInfo != null && customerAllInfo.getInvoName() != null
                && !("".equals(customerAllInfo.getInvoName().trim()))) {
            invoiceInformation.setName(customerAllInfo.getInvoName());
            invoiceInformation.setUniqueNo(customerAllInfo.getUniqueNo());
            invoiceInformation.setAddress(customerAllInfo.getInvoAddress());
            invoiceInformation.setTelephone(customerAllInfo.getInvoTelephone());
            invoiceInformation.setOpenBank(customerAllInfo.getOpenBank());
            invoiceInformation.setOpenBankNo(customerAllInfo.getOpenBankNo());
            invoiceInformation.setRemarks(customerAllInfo.getInvoRemarks());
            invoiceInformation.setCreateId(user.getId());
            invoiceInformation.setCreateName(user.getName());
            invoiceInformation.setCreateTime(nowTime);
            invoiceInformation.setIsEnable(customerAllInfo.getInvoIsEnable());

            invoiceInformationDao.insertAndGetId(invoiceInformation);
            company.setInvoiceId(invoiceInformation.getId());
        }

        if (customerAllInfo != null
                && customerAllInfo.getCompanyName() != null
                && !("".equals(customerAllInfo.getCompanyName().trim()))) {
            company.setName(customerAllInfo.getCompanyName());
            company.setEmail(customerAllInfo.getComEmail());
            company.setAddress(customerAllInfo.getComAddress());
            company.setNet(customerAllInfo.getNet());
            company.setRegCap(customerAllInfo.getRegCap());
            company.setNeed(customerAllInfo.getComNeed());
            company.setNature(customerAllInfo.getNature());
            company.setRemarks(customerAllInfo.getComRemarks());
            company.setCreateId(user.getId());
            company.setCreateName(user.getName());
            company.setCreateTime(nowTime);
            company.setIsEnable(customerAllInfo.getComIsEnable());

            companyDao.insertAndGetId(company);
            customer.setCompanyId(company.getId());
        }

        if (customerAllInfo != null
                && customerAllInfo.getCusName() != null
                && !("".equals(customerAllInfo.getCusName().trim()))) {
            customer.setName(customerAllInfo.getCusName());
            customer.setSex(customerAllInfo.getSex());
            customer.setContact1(customerAllInfo.getContact1());
            customer.setContact2(customerAllInfo.getContact2());
            customer.setEmail(customerAllInfo.getCusEmail());
            customer.setAddress(customerAllInfo.getCusAddress());
            customer.setBirthday(customerAllInfo.getBirthday());
            customer.setIdCard(customerAllInfo.getIdCard());
            customer.setInterest(customerAllInfo.getInterest());
            customer.setScore(customerAllInfo.getScore());
            customer.setNeed(customerAllInfo.getCusNeed());
            customer.setImpression(customerAllInfo.getImpression());
            // companyId
            // invoiceId
            customer.setRemarks(customerAllInfo.getCusRemarks());
            customer.setCreateId(user.getId());
            customer.setCreateName(user.getName());
            customer.setCreateTime(nowTime);
            customer.setIsEnable(customerAllInfo.getCusIsEnable());

            customerDao.insertAndGetId(customer);
        }

        return customer;
    }
}
